package com.sdry.web.controller.zc.quality;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcJzSpotCheckDetailsEntity;
import com.sdry.model.zc.ZcJzSpotCheckEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.zc.ZcJzSpotCheckService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcJzSpotCheckController   
 * @Description:  浇铸抽检
 * @Author:       zc   
 * @CreateDate:   2018年10月19日 上午10:10:01   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("jzSpotCheck")
public class ZcJzSpotCheckController {

	@Resource
	private ZcJzSpotCheckService zcJzSpotCheckService;
	/**
	 * 进入浇铸抽检页面
	 * @return
	 */
	@RequestMapping("enterJzSpotCheckPage")
	public String enterJzSpotCheckPage(){
		return "/zc/quality/jzSpotCheck";
	}
	/**
	 * 查询浇铸抽检单列表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectJzSpotCheckList")
	public void selectJzSpotCheckList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		if(zcQueryCriteria.getKeyword()==null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcJzSpotCheckEntity> selectMsSpotCheckList = zcJzSpotCheckService.selectJzSpotCheckList(zcQueryCriteria);
		int count = zcJzSpotCheckService.countJzSpotCheckList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectMsSpotCheckList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存浇铸记录表
	 * @param request
	 * @param response
	 * @param zcJzSpotCheckEntity
	 */
	@RequestMapping("insertJzSpotCheck")
	public void insertJzSpotCheck(HttpServletRequest request,HttpServletResponse response,ZcJzSpotCheckEntity zcJzSpotCheckEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcJzSpotCheckEntity.setCheckPerson(userName);
		zcJzSpotCheckEntity.setCheckStatus("0");
		int affact = zcJzSpotCheckService.inserJzSpotCheck(zcJzSpotCheckEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除浇铸抽检单
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delJzSpotCheckArr")
	public void delJzSpotCheckArr(HttpServletRequest request, HttpServletResponse response,@RequestParam("idArr[]") String[] idArr){
		int affact = 0;
		for (String did : idArr) {
			long id = Long.parseLong(did);
			affact += zcJzSpotCheckService.delJzSpotCheckArr(id);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 修改浇铸抽检单
	 * @param request
	 * @param response
	 * @param zcJzSpotCheckEntity
	 */
	@RequestMapping("updateJzSpotCheck")
	public void updateJzSpotCheck(HttpServletRequest request, HttpServletResponse response,ZcJzSpotCheckEntity zcJzSpotCheckEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcJzSpotCheckEntity.setCheckPerson(userName);
		zcJzSpotCheckEntity.setCheckStatus("0");
		int affact = zcJzSpotCheckService.updateJzSpotCheck(zcJzSpotCheckEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存浇铸记录详情
	 * @param request
	 * @param response
	 * @param zcJzSpotCheckDetailsEntity
	 */
	@RequestMapping("insertJzSpotCheckDetails")
	public void insertJzSpotCheckDetails(HttpServletRequest request,HttpServletResponse response,ZcJzSpotCheckDetailsEntity zcJzSpotCheckDetailsEntity){
		int affact = zcJzSpotCheckService.insertJzSpotCheckDetails(zcJzSpotCheckDetailsEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过检测序号查询详情
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectJzSpotCheckDetailsListByNumber")
	public void selectJzSpotCheckDetailsListByNumber(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcJzSpotCheckDetailsEntity> selectJzSpotCheckDetailsListByNumber = zcJzSpotCheckService.selectJzSpotCheckDetailsListByNumber(zcQueryCriteria);
		int count = zcJzSpotCheckService.countJzSpotCheckDetailsListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectJzSpotCheckDetailsListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询浇铸抽检统计表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectJzSpotCheckStatisticsTable")
	public void selectJzSpotCheckStatisticsTable(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcJzSpotCheckDetailsEntity> selectJzSpotCheckStatisticsTable = zcJzSpotCheckService.selectJzSpotCheckStatisticsTable(zcQueryCriteria);
		int count = zcJzSpotCheckService.countJzSpotCheckStatisticsTable(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectJzSpotCheckStatisticsTable, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过编码查组件型号
	 * @param request
	 * @param response
	 * @param number
	 */
	@RequestMapping("selectPcarByNumber")
	public void selectPcarByNumber(HttpServletRequest request,HttpServletResponse response,String number){
		String pcar = zcJzSpotCheckService.selectPcarByNumber(number);
		try {
			ResponseUtil.write(response, pcar);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
